package site.shihuan.helpdesk.im.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.im.SessionContextHolder;
import site.shihuan.helpdesk.im.manager.VisitorManager;
import site.shihuan.helpdesk.im.message.impl.ContactMessage;

/**
 * 客服API
 *
 * @author 周世焕
 * @Date 2021/4/12 16:21
 */
@RestController
@RequestMapping("/v1/im")
public class ServiceController {

    @Autowired
    private SessionContextHolder sessionContextHolder;

    @Autowired
    private VisitorManager visitorManager;

    @PostMapping("service/finish")
    public RestResult finishService(@RequestParam String visitorId) {
        ContactMessage message = new ContactMessage();
        message.setCmd(ContactMessage.CMD_REMOVE_CONTACT);
        message.setPayload(visitorId);
        sessionContextHolder.sendMessage(message,visitorId);
        return RestResultUtils.success();
    }

    @PostMapping("service/start")
    public RestResult startService(@RequestParam String tenantId,@RequestParam String visitorId) {
        /**
         * 通知客服组
         */
        ContactMessage message = new ContactMessage();
        message.setCmd(ContactMessage.CMD_REMOVE_VISITOR);
        message.setPayload(visitorId);
        sessionContextHolder.broadcastAllClerk(message,tenantId);
        /**
         * 通知访客已接入
         */
        message.setCmd(ContactMessage.CMD_ADD_CONTACT);
        message.setPayload(visitorManager.findMyDeskClerk(visitorId,tenantId));
        sessionContextHolder.sendMessage(message,visitorId);
        return RestResultUtils.success();
    }
}
